﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data;

namespace DataAccessLayer
{
    class ManagerDao
    {
        #region Variables
        protected static Database database = null;
        private static DbConnection connection = null;
        protected static DbTransaction transaction = null;
        #endregion

        #region Properties
        public static Database Database
        {
            get
            {
                if (database == null)
                {
                    database = DatabaseFactory.CreateDatabase();
                }
                return database;
            }
        }

        public static DbConnection Connection
        {
            get
            {
                if (connection == null)
                {
                    connection = Database.CreateConnection();
                    connection.Open();
                }
                if (connection.State.Equals(ConnectionState.Closed))
                {
                    connection.Open();
                }
                return connection;
            }
        }
        public static DbTransaction Transaction
        {
            get
            {
                if (transaction == null)
                {
                    transaction = Connection.BeginTransaction();
                }
                return transaction;
            }
        }
        #endregion

        #region Methods

        public static DbCommand GetSqlStringCommand(string sqlString)
        {
            DbCommand cmd = Database.GetSqlStringCommand(sqlString);
            if (transaction != null)
                cmd.Transaction = transaction;
            return cmd;
        }

        public static DbCommand GetStoredProcCommand(string spName)
        {
            return Database.GetStoredProcCommand(spName);
        }

        public static void AddInParameter(DbCommand cmd, string name, DbType dbType, object value)
        {
            Database.AddInParameter(cmd, name, dbType, value);
        }

        public static void AddParameter(DbCommand cmd, string name, DbType dbType, ParameterDirection paramDirection, string sourceColumn, DataRowVersion dataRowVersion, object value)
        {
            Database.AddParameter(cmd, name, dbType, paramDirection, sourceColumn, dataRowVersion, value);
        }

        public static object GetParameterValue(DbCommand cmd, string name)
        {
            return database.GetParameterValue(cmd, name);
        }

        public static int ExecuteNonQuery(DbCommand cmd)
        {
            if (transaction != null)
            {
                return Database.ExecuteNonQuery(cmd, transaction);
            }
            else
            {
                return Database.ExecuteNonQuery(cmd);
            }
        }

        public static IDataReader ExecuteReader(DbCommand cmd)
        {
            if (transaction != null)
            {
                return Database.ExecuteReader(cmd, transaction);
            }
            else
            {
                return Database.ExecuteReader(cmd);
            }
        }

        public static object ExecuteScalar(DbCommand cmd)
        {
            if (transaction != null)
            {
                return Database.ExecuteScalar(cmd, transaction);
            }
            else
            {
                return Database.ExecuteScalar(cmd);
            }
        }

        public static void BeginTransaction()
        {
            transaction = Connection.BeginTransaction();
        }

        public static void Commit()
        {
            if (transaction != null)
            {
                transaction.Commit();
                transaction = null;
            }
        }

        public static void Rollback()
        {
            if (transaction != null)
            {
                transaction.Rollback();
                transaction = null;
            }
        }
        public static Guid GetguidDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return new Guid(reader[field].ToString());
            }
            catch
            {
            }
            return Guid.Empty;
        }
        public static string GetstringDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return reader[field].ToString();
            }
            catch
            {
            }
            return string.Empty;
        }
        public static bool GetboolDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return (Boolean)reader[field];
            }
            catch
            {
            }
            return false;
        }
        public static Int32 Getint32DataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return int.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return Int32.MinValue;
        }
        public static byte GetbyteDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return byte.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return byte.MinValue;
        }
        public static Int32 GetintDataFromDataReader(IDataReader reader, string field)
        {
            return Getint32DataFromDataReader(reader, field);
        }
        public static Int16 Getint16DataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return Int16.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return Int16.MinValue;
        }
        public static Int64 Getint64DataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return Int64.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return Int64.MinValue;
        }
        public static Decimal GetdecimalDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return Decimal.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return Decimal.MinValue;
        }
        public static Double GetdoubleDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return Double.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return Double.MinValue;
        }
        public static float GetfloatDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return float.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return float.MinValue;
        }

        public static DateTime GetdatetimeDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return DateTime.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return DateTime.MinValue;
        }
        public static Boolean GetbooleanDataFromDataReader(IDataReader reader, string field)
        {
            try
            {
                if (!reader.IsDBNull(reader.GetOrdinal(field)))
                    return Boolean.Parse(reader[field].ToString());
            }
            catch
            {
            }
            return false;
        }




        #endregion
    }
}
